------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

    胡桃专属 classified 。用来挂载 netvars

    客户端这边，通过监听
        player:PushEvent("hutao_classified_inited")
    来确保可以使用 classified

]]--
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- 注册去replica  AttachClassified
    local replica_com = {
        ["hutao_com_level_sys"] = true,                     --  等级
        ["hutao_com_physical_power"] = true,                -- 体力
        ["hutao_com_polearm_mastery"] = true,                -- 长柄精通
        ["hutao_com_butterfly_s_embrace"] = true,            -- 蝶引来生 （Butterfly's Embrace）
        ["hutao_com_healing_grove"] = true,                 -- 郁愈域
        ["hutao_com_cosmic_hat_sys_for_player"] = true,     --  乾坤帽
        ["hutao_com_inspect_data"] = true,                  --  检查界面数据库
    }
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- hutao_com_inspect_data 检查界面数据库
    local function init_inspect_data_api(inst)
        inst.__inspect_data = net_string(inst.GUID,"hutao_classified_inspect_data","hutao_classified_inspect_data_update")
        inst.__inspect_data:set(json.encode({}))
        inst.SetInspectData = function(inst,data_table)
            if TheWorld.ismastersim then
                inst.__inspect_data:set(json.encode(data_table))
            end
        end
        inst.GetInspectData = function(inst)
            return json.decode(inst.__inspect_data:value())
        end
    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- hutao_com_cosmic_hat_sys_for_player 【乾坤帽】
    local function init_cosmic_hat_sys_for_player_api(inst)
        --- 帽子本体
        inst.__cosmic_hat_inst = net_entity(inst.GUID,"hutao_classified_cosmic_hat_inst","cosmic_hat_sys_com_update")
        inst.SetCosmicHatInst = function(inst,cosmic_hat_inst)
            if TheWorld.ismastersim then
                inst.__cosmic_hat_inst:set(cosmic_hat_inst)
            end
        end
        inst.GetCosmicHatInst = function(inst)
            return inst.__cosmic_hat_inst:value()
        end
        --- 晾晒架
        inst.__cosmic_hat_drying_rack_inst = net_entity(inst.GUID,"hutao_classified_cosmic_hat_drying_rack_inst","cosmic_hat_sys_com_update")
        inst.SetCosmicHatDryingRackInst = function(inst,cosmic_hat_drying_rack_inst)
            if TheWorld.ismastersim then
                inst.__cosmic_hat_drying_rack_inst:set(cosmic_hat_drying_rack_inst)
            end
        end
        inst.GetCosmicHatDryingRackInst = function(inst)
            return inst.__cosmic_hat_drying_rack_inst:value()
        end

    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- hutao_com_healing_grove 【郁愈域】
    local function init_healing_grove_api(inst)
        inst.__healing_grove_current = net_float(inst.GUID,"hutao_classified_healing_grove_current","healing_grove_com_update")
        inst.SetHealingGroveCurrent = function(inst,healing_grove_current)
            if TheWorld.ismastersim then
                inst.__healing_grove_current:set(healing_grove_current)
            end
        end
        inst.GetHealingGroveCurrent = function(inst)
            return inst.__healing_grove_current:value()
        end
    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- hutao_com_butterfly_s_embrace 蝶引来生 （Butterfly's Embrace）
    local function init_butterfly_s_embrace_api(inst)
        --- debuff inst
        inst.__butterfly_s_embrace = net_entity(inst.GUID,"hutao_classified_butterfly_s_embrace","butterfly_s_embrace_com_update")
        inst.SetButterflySEmbrace = function(inst,butterfly_s_embrace)
            if TheWorld.ismastersim then
                inst.__butterfly_s_embrace:set(butterfly_s_embrace)
            end
        end
        inst.GetButterflySEmbrace = function(inst)
            return inst.__butterfly_s_embrace:value()
        end
        --- 血量消耗
        inst.__butterfly_s_embrace_health_cost = net_float(inst.GUID,"hutao_classified_butterfly_s_embrace_health_cost","butterfly_s_embrace_com_update")
        inst.SetButterflySEmbraceHealthCost = function(inst,butterfly_s_embrace_health_cost)
            if TheWorld.ismastersim then
                inst.__butterfly_s_embrace_health_cost:set(butterfly_s_embrace_health_cost)
            end
        end
        inst.GetButterflySEmbraceHealthCost = function(inst)
            return inst.__butterfly_s_embrace_health_cost:value()
        end
        --- 持续时间
        inst.__butterfly_s_embrace_debuff_remain_time = net_float(inst.GUID,"hutao_classified_butterfly_s_embrace_debuff_remain_time","butterfly_s_embrace_com_update")
        inst.SetButterflySEmbraceDebuffRemainTime = function(inst,time)
            if TheWorld.ismastersim then
                inst.__butterfly_s_embrace_debuff_remain_time:set(time)
            end
        end
        inst.GetButterflySEmbraceDebuffRemainTime = function(inst)
            return inst.__butterfly_s_embrace_debuff_remain_time:value()
        end
        --- 激活时间
        inst.__butterfly_s_embrace_debuff_active_time  = net_float(inst.GUID,"hutao_classified_butterfly_s_embrace_debuff_active_time","butterfly_s_embrace_com_update")
        inst.SetButterflySEmbraceDebuffActiveTime = function(inst,time)
            if TheWorld.ismastersim then
                inst.__butterfly_s_embrace_debuff_active_time:set(time)
            end
        end
        inst.GetButterflySEmbraceDebuffActiveTime = function(inst)
            return inst.__butterfly_s_embrace_debuff_active_time:value()
        end
    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- hutao_com_polearm_mastery 长柄精通
    local function init_polearm_mastery_api(inst)
        --- 能量
        inst.polearm_mastery = net_float(inst.GUID,"hutao_classified_polearm_mastery","polearm_mastery_com_update")
        inst.SetPolearmMastery = function(inst,polearm_mastery)
            if TheWorld.ismastersim then
                inst.polearm_mastery:set(polearm_mastery)
            end
        end
        inst.GetPolearmMastery = function(inst)
            return inst.polearm_mastery:value()
        end
        --- 能量max
        inst.polearm_mastery_max = net_float(inst.GUID,"hutao_classified_polearm_mastery_max","polearm_mastery_com_update")
        inst.polearm_mastery_max:set(12) -- 创建的时候需要立马赋予一个值
        inst.SetPolearmMasteryMax = function(inst,polearm_mastery_max)
            if TheWorld.ismastersim then
                inst.polearm_mastery_max:set(polearm_mastery_max)
            end
        end
        inst.GetPolearmMasteryMax = function(inst)
            return inst.polearm_mastery_max:value()
        end
        --- 武器
        inst.polearm_mastery_data = net_string(inst.GUID,"hutao_classified_polearm_mastery_data","hutao_classified_polearm_mastery_data")
        inst.SetPolearmMasteryData = function(inst,polearm_mastery_data)
            if TheWorld.ismastersim then
                local json_str = json.encode(polearm_mastery_data or {})
                inst.polearm_mastery_data:set(json_str)
            end
        end
        inst.GetPolearmMasteryData = function(inst)
            local json_str = inst.polearm_mastery_data:value()
            local succeed_flag,ret = pcall(json.decode,json_str)
            if succeed_flag then
                return ret
            else
                return {}
            end
        end
        
    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- physical_power 体力系统
    local function init_physical_power_api(inst)
        ---- 体力
        inst.physical_power = net_float(inst.GUID,"hutao_classified_physical_power","physical_power_com_update")
        inst.SetPhysicalPower = function(inst,physical_power)
            if TheWorld.ismastersim then
                inst.physical_power:set(physical_power)
            end
        end
        inst.GetPhysicalPower = function(inst)
            return inst.physical_power:value()
        end
        ---- 体力max
        inst.physical_power_max = net_float(inst.GUID,"hutao_classified_physical_power_max","physical_power_com_update")
        inst.physical_power_max:set(100) -- 创建的时候需要立马赋予一个值
        inst.SetPhysicalPowerMax = function(inst,physical_power_max)
            if TheWorld.ismastersim then
                inst.physical_power_max:set(physical_power_max)
            end
        end
        inst.GetPhysicalPowerMax = function(inst)
            return inst.physical_power_max:value()
        end
        --- running
        inst.physical_power_running = net_bool(inst.GUID,"hutao_classified_physical_power_running","hutao_classified_physical_power_running")
        inst.SetPhysicalPowerRunning = function(inst,physical_power_running)
            if TheWorld.ismastersim then
                inst.physical_power_running:set(physical_power_running)
            end
        end
        inst.GetPhysicalPowerRunning = function(inst)
            return inst.physical_power_running:value()
        end
        --- running event , for spear spell
        inst:ListenForEvent("hutao_classified_physical_power_running",function()
            local player = inst.owner
            if player then
                player:PushEvent("hutao_classified_physical_power_running",inst:GetPhysicalPowerRunning())
            end
        end)
    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- level + exp
    local function init_level_api(inst)
        ---- 等级
        inst.level = net_uint(inst.GUID,"hutao_classified_level","level_com_update")
        inst.SetLevel = function(inst,level)
            if TheWorld.ismastersim then
                inst.level:set(level)
            end
        end
        inst.GetLevel = function(inst)
            return inst.level:value()
        end
        ---- 经验
        inst.exp = net_float(inst.GUID,"hutao_classified_exp","level_com_update")
        inst.SetExp = function(inst,exp)
            if TheWorld.ismastersim then
                inst.exp:set(exp)
            end
        end
        inst.GetExp = function(inst)
            return inst.exp:value()
        end
        ---- 经验max
        inst.exp_max = net_float(inst.GUID,"hutao_classified_exp_max","level_com_update")
        inst.exp_max:set(5) -- 创建的时候需要立马赋予一个值
        inst.SetExpMax = function(inst,exp_max)
            if TheWorld.ismastersim then
                inst.exp_max:set(exp_max)
            end
        end
        inst.GetExpMax = function(inst)
            return inst.exp_max:value()
        end
        ---- data
        inst.level_data = net_string(inst.GUID,"hutao_classified_level_data","level_com_update")
        inst.level_data:set(json.encode({})) -- 创建的时候需要立马赋予一个值
        inst.SetLevelData = function(inst,level_data)
            if TheWorld.ismastersim then
                local json_str = json.encode(level_data)
                inst.level_data:set(json_str)
            end
        end
        inst.GetLevelData = function(inst)
            local json_str = inst.level_data:value()
            local flag,data = pcall(json.decode,json_str)
            if flag then
                return data
            end
        end
    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- init API
    local function init_event(inst,_player)
        if inst.owner then
            return
        end
        local player = _player or inst.__init:value()
        -- print("666666666666666666",player)
        if TheWorld.ismastersim then
            inst.entity:SetParent(player.entity)
            inst.Transform:SetPosition(0,0,0)
            inst:DoTaskInTime(10,function()
                --- 不能一开始就设置这个参数，会直接导致客户端这边无法初始化
                inst.Network:SetClassifiedTarget(player)
            end)
        end
        inst.owner = player
        --- 注册去replica  AttachClassified
        for com_name, flag in pairs(replica_com) do
            if com_name and flag and (player.replica[com_name] or player.replica._[com_name]) then
                local com = player.replica[com_name] or player.replica._[com_name]
                if com.AttachClassified then
                    com:AttachClassified(inst)
                end
            end
        end
        inst:PushEvent("inited")
        print("info hutao_classified inited for",player)
        player.hutao_classified = inst
        player:PushEvent("hutao_classified_inited") --- 通知玩家。可用来触发 界面安装。
    end
    local function init_api_install(inst)
        inst.__init = net_entity(inst.GUID,"hutao_classified_init","hutao_classified_init")
        inst:ListenForEvent("hutao_classified_init",init_event)
        inst.Init = function(inst,player)
            if TheWorld.ismastersim then
                inst.__init:set(player)
                init_event(inst,player)
            end
            -- print("fake error hutao classified init for",player)
        end
        --- 用来处理某些极端情况下，无法在客户端初始化绑定classified，或者服务端这边，重复多次创建classified，导致无法唯一化绑定
        inst.__init_task = inst:DoPeriodicTask(1,function(inst)
            -- print("warning : hutao_classified init task",inst,inst.owner)
            if TheWorld.ismastersim and inst.owner == nil then
                inst:Remove()
                -- print("error : hutao_classified init task remove",inst)
                return
            end
            if inst.owner then
                inst.__init_task:Cancel()
                -- print("warning : hutao_classified has inited")
                return
            end
            local player = inst.entity:GetParent()
            if player and player:IsValid() then
                init_event(inst,player)
            end
        end)
    end
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddNetwork()
    -- inst.entity:Hide()
    inst:AddTag("CLASSIFIED")
    inst.entity:SetPristine()
    -------------------------------------------------------
    ---

    -------------------------------------------------------
    ---
        init_api_install(inst)
        init_level_api(inst)
        init_physical_power_api(inst)
        init_polearm_mastery_api(inst)
        init_butterfly_s_embrace_api(inst)
        init_healing_grove_api(inst)
        init_cosmic_hat_sys_for_player_api(inst)
        init_inspect_data_api(inst)
    -------------------------------------------------------
    if not TheWorld.ismastersim then
        -- inst.OnEntityReplicated = init_event
        return inst
    end

    return inst
end
return Prefab("hutao_classified", fn)
